package com.my.user.web;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.my.user.entity.User;
import com.my.user.service.UserService;
import com.my.user.service.UserServiceImpl;

@WebServlet("/UserInfoUpdateAction")
public class UserInfoUpdateAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		User user=new User();
		request.setCharacterEncoding("utf-8");
		String id_str=request.getParameter("uId");
		Integer id=Integer.parseInt(id_str);
		System.out.println(id);
		user.setuId(id);
		user.setuName(request.getParameter("uName"));
		user.setuPassword(request.getParameter("uPassword"));

		
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date date=null;
		String date_str=request.getParameter("uBirthday");
		try{	
			date=sdf.parse(date_str);
		}catch(Exception e){
			System.out.println("转换日期发生异常"+e.getMessage());
	
		}
		user.setuBirthday(date);
		user.setuAddress(request.getParameter("uAddress"));
		user.setuQqNumber(request.getParameter("uQqNumber"));
		user.setuPhoneNumber(request.getParameter("uPhoneNumber"));
		//user.setuSex(request.getParameter("uSex"));
		user.setuEMail(request.getParameter("uEMail"));
		
		UserService us=new UserServiceImpl();
		boolean flag=us.updateOne(user);
		
		if(flag){
			User user1=us.selectOneById(user.getuId());
			request.setAttribute("user",user1);
			request.getRequestDispatcher("userinfo.jsp").forward(request,response);
		}else{
			request.getRequestDispatcher("updateerror.jsp").forward(request,response);
		}
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		doGet(request, response);
	}
	
}
